'use strict';
const {parseNum} = require("../../utils");
const {getDefaultModelConfig} = require("../../utils/common");

module.exports = app => {
    const { STRING, INTEGER, DECIMAL, TEXT, TINYINT } = app.Sequelize;
    const TravelOrderItem = app.model.define(
        'TravelOrderItem',
        {
            id: {type: INTEGER, primaryKey: true, autoIncrement: true},
            travel_order_id: {type: INTEGER, allowNull: false, comment: '旅游订单ID'},
            travel_id: {type: INTEGER, allowNull: false, comment: '旅游产品ID'},
            travel_sku_id: {type: INTEGER, allowNull: true, comment: '旅游产品SKU ID'},
            travel_sku_stock_id: {type: INTEGER, allowNull: true, comment: '旅游产品SKU库存ID'},
            travel_name: {type: STRING(255), allowNull: false, comment: '旅游产品名称'},
            travel_cover: {type: STRING(500), allowNull: true, comment: '旅游产品封面'},
            sku_spec: {type: TEXT, allowNull: true, comment: 'SKU规格信息，JSON格式'},
            refund_status: {
                type: TINYINT,
                defaultValue: -1,
                comment: '退款状态：-1-未退款，0-退款失败，1-待处理 2退款中 3 退款成功'
            },
            price: {
                type: DECIMAL(10, 2),
                defaultValue: 0.00,
                comment: '产品单价',
                get() {
                    return parseNum(this.getDataValue('price'));
                }
            },
            count: {type: INTEGER, defaultValue: 1, comment: '购买数量'},
            total_amount: {
                type: DECIMAL(10, 2),
                defaultValue: 0.00,
                comment: '小计金额',
                get() {
                    return parseNum(this.getDataValue('total_amount'));
                }
            },
            traveler_name: {type: STRING(255), allowNull: true, comment: '出行人姓名'},
            traveler_id_card: {type: STRING(255), allowNull: true, comment: '出行人身份证号'},
            traveler_phone: {type: STRING(255), allowNull: true, comment: '出行人手机号'},
            traveler_type: {type: STRING(255), allowNull: true, comment: '出行人类型'},
            ...getDefaultModelConfig(app)
        },
        {
            tableName: 'travel_order_item',
            timestamps: true,
            createdAt: 'create_time',
            updatedAt: 'update_time'
        }
    );

    TravelOrderItem.associate = function() {
        app.model.Business.TravelOrderItem.belongsTo(app.model.Business.TravelOrder, {as: 'travelOrder', foreignKey: 'travel_order_id'});
        app.model.Business.TravelOrderItem.belongsTo(app.model.Business.Travel, {as: 'travel', foreignKey: 'travel_id'});
        app.model.Business.TravelOrderItem.belongsTo(app.model.Business.TravelSku, {as: 'travelSku', foreignKey: 'travel_sku_id'});
    }
    
    return TravelOrderItem;
}; 